/**
 * Created by Administrator on 2018/8/17.
 */
// 资产库可用资产信息
var purchaseProductDatas = new Array();
var purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}

var bqArray = {};
$CertusJsonAjax.ajax("purchaseBaseInfoAndQuote",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){
    if(res.data){
        var buyings = res.data.buyings;
        ;
        for(var i in buyings){
            bqArray[buyings[i].biCode] = buyings[i].buyingQuoteItemEntities;
        }
    }
})



$$scope.submmitData = {};
$$scope.submmitData.purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
$$scope.select = {};
// 采购记录
var proc1Columns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '品名及规格',
        width: 200,
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'quantity',
        title: '数量',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'estimatedAmount',
        title: '预计金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 220,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-primary" onclick="hdplog(\'' + record.biCode + '\',\'' + record.commodityClassCode + '\');"><i class="icon-eye-open"></i>  报价历史库</button>';
            content += '  ';
            0
            content += '<button class="btn btn-xs btn-success" onclick="showNowQuote(\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  报价</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-info" onclick="newQuote(\'' + record.biCode + '\')"><i class="icon-plus"></i>  报价</button>';
            return content;
        }
    }];

//查詢歷史 報價 列表
function hdplog(biCode, commodityClassCode) {
    $CertusFormAjax.ajax("findHistoryQuote", {biCode: biCode, commodityClassCode: commodityClassCode}, function (res) {
        middleGrid.exhibitDatas = res.data;
        middleGrid.refresh(false);
        $(".hdplog").show();
    })
}


function showNowQuote(biCode) {
    if (biCode) {
        belowGrid.exhibitDatas = bqArray[biCode];
        belowGrid.refresh(false);
    }
}

//打开 历史 报价


function showAllQuote() {
    var tempArray = new Array();
    if (bqArray) {
        for (var j in bqArray) {
            tempArray = tempArray.concat(bqArray[j]);
        }
    }
    belowGrid.exhibitDatas = tempArray;
    belowGrid.refresh(false);
}

function newQuote(biCode) {
    var exhibitDatas = aboveGrid.exhibitDatas;
    for (var i in exhibitDatas) {
        if (exhibitDatas[i].biCode == biCode) {
            var insertTemp = exhibitDatas[i];
            $$scope.buyingQuoteItem = {};
            $$scope.buyingQuoteItem = {
                "commodityCateName": insertTemp.commodityCateName,
                "commodityClassName": insertTemp.commodityClassName,
                "name": insertTemp.name,
                "biCode": insertTemp.biCode,
                "brCode": insertTemp.brCode,
                "bqCode": "Bq-" + new Date().getTime() + Math.floor(Math.random() * 10000)
            }
            $$scope.$apply();
            $(".hdgs").show();
        }
    }
}


$$scope.saveQuote = function () {

    var biCode = $$scope.buyingQuoteItem.biCode;
    if (biCode) {
        //已有的缓存数组
        var bqArray2 = bqArray[biCode];
        var alreadyHas = false;
        if (bqArray2) {
            for (var i in bqArray2) {
                if (bqArray2[i].bqCode == $$scope.buyingQuoteItem.bqCode) {
                    bqArray2[i] = $$scope.buyingQuoteItem
                    alreadyHas = true;
                    1
                }
            }
        }
        if (alreadyHas == true) {
            belowGrid.exhibitDatas = bqArray2;
        } else {
            bqArray2.push($$scope.buyingQuoteItem);
            belowGrid.exhibitDatas = bqArray2;
        }
        belowGrid.refresh(false);
        $(".hdgs").hide();
    } else {
        layer.msg("没有bicode");
    }
}
// 供应商报价列表
var prcListColumns = [
//     {
//     id: 'no',
//     title: '序号',
//     width: 50,
//     type: 'string',
//     columnClass: 'text-center',
// }, 
{
    id: 'commodityCateName',
    title: '类目',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'commodityClassName',
    title: '类别',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'address',
    title: '地点',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'supplierName',
    title: '供应商名称',
    // width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'supplierPhone',
    title: '联系方式',
    width: 110,
    type: 'string',
    columnClass: 'text-center',
},
    {
        id: 'specification',
        title: '规格(参数)',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'price',
        title: '价格(说明)',
        // width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'priceIncludingTax',
        title: '价格(含税)',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'dateOfDelivery',
        title: '交货期',
        width: 80,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'guaranteeTime',
        title: '质保期',
        width: 74,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'paymentMethod',
        title: '付款方式',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'strengthsAndWeaknesses',
        title: '优劣势',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'operation',
        title: '操作',
        width: 240,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-info" onclick="shproc(\'' + record.bqCode + '\',\'' + record.biCode + '\');"><i class="fa fa-edit"></i>  编辑</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-success" onclick="shupload(\'' + record.bqCode + '\',\'' + record.biCode + '\');"><i class="icon-cloud-upload"></i>  附件</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-primary" onclick="openDownloadList(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  附件</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-danger" onclick="del(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }
];

//打开 下载 列表
function openDownloadList(bqCode, biCode) {
    $$scope.modalContent = {};
    if(biCode && bqCode){
        var nowBq = bqArray[biCode];
        if(nowBq){
            for(var i in nowBq){
                if(bqCode == nowBq[i].bqCode){
                    if(nowBq[i].filePathArray){
                        $$scope.modalContent = nowBq[i].filePathArray
                    }else if(nowBq[i].supplierFile){
                        $$scope.modalContent = eval(nowBq[i].supplierFile);
                    }
                }

            }
        }
        $$scope.$apply();
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}

var fileInstantiation = {
    classCateCode: 'WBLY',
    fileClassCode: 'KHBJD'
}
var fileInstantiationDisable = {
    classCateCodeDis: true, //文件类目不可修改
    fileClassCodeDis: true //文件类型不可修改
}
//调用初始化方法
initFileUpload($$scope, fileInstantiation, fileInstantiationDisable, $CertusFormAjax);


function shupload(bqCode, biCode) {
    $$scope.nowUploadBqcode = bqCode;
    $$scope.nowUploadBicode = biCode;
    //上传附件
    $(".hdupload").show();
}

//上传文件
$$scope.uploadBqFile = function (a, b) {

    fileUpload($$scope, function (res) {

        if (res.data) {
            //存入临时 变量中
            if (bqArray) {
                if (bqArray[b]) {
                    var bqArray2 = bqArray[b];
                    for (var i in bqArray2) {
                        if (bqArray2[i]) {
                            if (bqArray2[i].bqCode == a) {
                                if (!bqArray2[i].filePathArray) {
                                    bqArray2[i].filePathArray = new Array();
                                }
                                var da = {
                                    "fileId": res.data,
                                    "fileName": $$scope.file.fileName,
                                    "createTime":new Date()
                                }
                                bqArray2[i].filePathArray.push(da);
                                layer.msg("文件上传成功");
                                $(".hdupload").hide();
                                return;
                            }
                        }
                    }
                }
            }
        }
        layer.msg("文件上传失败");
    }, null, $CertusFormAjax);
}

function hdgs() {
    $(".hdgs").hide();
}
// 历史报价库
var pricelogColumns = [
//     {
//     id: 'no',
//     title: '序号',
//     width: 50,
//     type: 'string',
//     columnClass: 'text-center',
// },  
{
    id: 'pl1',
    title: '类目',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
},{
    id: 'pl2',
    title: '类别',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
},{
    id: 'plname',
    title: '名称',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
},{
    id: 'p2',
    title: '供应商名称',
    // width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'p3',
    title: '联系方式',
    width: 110,
    type: 'string',
    columnClass: 'text-center',
},
    {
        id: 'p4',
        title: '规格(参数)',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType:'lg|md|sm|xs',
    },
    {
        id: 'p5',
        title: '价格(说明)',
        // width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'p6',
        title: '价格(含税)',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'dateOfDelivery',
        title: '交货期',
        width: 80,
        type: 'string',
        columnClass: 'text-left',
        hideType:'lg|md|sm|xs',
    }, {
        id: 'p8',
        title: '质保期',
        width: 70,
        type: 'string',
        columnClass: 'text-left',
        hideType:'lg|md|sm|xs',
    }, {
        id: 'p9',
        title: '付款方式',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType:'lg|md|sm|xs',
    },
    {
        id: 'p10',
        title: '优劣势',
        type: 'string',
        columnClass: 'text-left',
        hideType:'lg|md|sm|xs',
    },
    {
        id: 'operation',
        title: '操作',
        width: 130,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-info" onclick="pDowload('+ record.id +')"><i class="icon-cloud-download"></i>  附件</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-info" onclick="pDowload('+ record.id +')"><i class="icon-cloud-download"></i>  协议</button>';
            // content += '  ';
            // content += '<button class="btn btn-xs btn-info" onclick="shgs();"><i class="icon-plus"></i>  报价</button>';
            return content;
        }
    }];



function shproc(bqCode, brCode) {
    if (bqCode && brCode) {

        var bqArray2 = bqArray[brCode];
        for (var i in bqArray2) {
            if (bqArray2[i].bqCode == bqCode) {
                $$scope.buyingQuoteItem = bqArray2[i];
                $$scope.$apply();
                $(".hdgs").show();
                return;
            }
        }
    }
}

var belowGrid;
var middleGrid;
var aboveGrid;
var brCode;
$CertusJsonAjax.ajax("purchaseBaseInfo", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    if (res.data) {

        $$scope.headerMap = res.data.headerMap;
        brCode = res.data.headerMap.brCode;
        purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;

        $$scope.submmitData.purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.projId = res.data.headerMap.projId;
        purchaseStepEntity.projId = res.data.headerMap.projId;
        var datas = res.data.buyings;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '采购单条目信息',
            datas: datas,
            columns: proc1Columns,
            gridContainer: 'proc3Container',
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();
        if (datas) {
            for (var i in datas) {
                if (datas[i].biCode) {
                    bqArray[datas[i].biCode] = new Array();
                }
            }
        }


        var pricelogOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '历史报价库',
            datas: [],
            columns: pricelogColumns,
            gridContainer: 'pricelogContainer',
            toolbarContainer: 'pricelogToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        middleGrid = $.fn.dlshouwen.grid.init(pricelogOption);
        middleGrid.load();

        var prcListOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '资产信息',
            datas: [],
            columns: prcListColumns,
            gridContainer: 'prcListContainer',
            toolbarContainer: 'prcListToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
        belowGrid.load();
    }
})
 $$scope.submitQuote = function(){

     if(certusValidateNotnull()){
         $$scope.submitter = {};
         $$scope.submitter.purchaseStepEntity = purchaseStepEntity;
         $$scope.submitter.buyingQuoteItemEntities =bqArray;
         $CertusJsonAjax.ajax("submitterProjQGquote",$$scope.submitter,function(res){
             if(res.data){
                 if(res.data == "success"){
                     findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                     window.history.back();
                     return;
                 }
                 layer.msg(res.data);
             }
         })
     }
 }

//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}
